#ifndef config
#define config


#include <iostream>
#include <string>
#include <memory>
#include <mutex>
#include <fstream>
#include <jsoncpp/json/json.h>
#include "jsonUtil.hpp"
#include "fileUtil.hpp"
namespace bucai
{
    class Config
    {
    private:
        int _hot_time;
        int _server_port;
        std::string _server_ip;
        std::string _url_prefix;
        std::string _arc_suffix;
        std::string _pack_dir;
        std::string _back_dir;
        std::string _manager_file;

        Json::Value _value;
        std::string _filename = "./httplib.conf";
        static std::mutex _mutex;
        static Config *_myjson;

    private:
        Config()
        {
            std::string str1;
            std::ifstream ifs;

            bucai::fileUtil fu(_filename);
            // bucai::fileUtil fu("/home/bucai/cloud/myConfig/fileUitl.hpp");
            if (fu.Exists() == false)
            {
                std::cout << "file is not exits" << std::endl;
                std::cout << _filename << std::endl;
            }
          
            fu.GetContent(&str1);

            bucai::jsonUtil json;
            json.UnSerialize(str1,&_value);

            _hot_time = _value["hot_time"].asInt();            
            _server_port = _value["server_port"].asInt();
            _server_ip = _value["server_ip"].asString();
            _url_prefix = _value["url_prefix"].asString();
            _arc_suffix = _value["arc_suffix"].asString();
            _pack_dir = _value["pack_dir"].asString();
            _back_dir = _value["back_dir"].asString();
            _manager_file = _value["manager_file"].asString();
        }

    public:
        static Config *getConfig() // 懒汉单例获取
        {
            if (_myjson == nullptr)
            {
                _mutex.lock();
                if (_myjson == nullptr)
                {
                    _myjson = new Config();
                }
                _mutex.unlock();
            }
            return _myjson;
        }

    public:
        int GetHotTime()
        {
            return _hot_time;
        }
        int GetServerPort()
        {
            return _server_port;
        }
        std::string GetServerIp()
        {
            return _server_ip;
        }
        std::string GetURLPrefix()
        {
            return _url_prefix;
        }
        std::string GetArcSuffix()
        {
            return _arc_suffix;
        }
        std::string GetPackDir()
        {
            return _pack_dir;
        }
        std::string GetBackDir()
        {
            return _back_dir;
        }
        std::string GetManagerFile()
        {
            return _manager_file;
        }
    };
    Config *Config::_myjson = nullptr;
    std::mutex Config::_mutex;
}

#endif
// int main()
// {
//     std::string filename = "./httplib.conf";
//     bucai::Config *con = bucai::Config::getConfig(filename);

//     std::cout << con->GetArcSuffix() << std::endl;
//     std::cout << con->GetBackDir() << std::endl;
//     std::cout << con->GetHotTime() << std::endl;
//     std::cout << con->GetManagerFile() << std::endl;
//     std::cout << con->GetPackDir() << std::endl;
//     std::cout << con->GetServerIp() << std::endl;
//     std::cout << con->GetServerPort() << std::endl;
//     std::cout << con->GetURLPrefix() << std::endl;

//     return 0;
// }

